<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="login-title">欢迎登录</h1>
      <el-form
          ref="form"
          :model="form"
          label-width="80px"
          size="medium"
          class="login-form"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              clearable
              autofocus
              @keyup.enter="onLogin"
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label="密 码" prop="password">
          <el-input
              v-model="form.password"
              type="password"
              show-password
              placeholder="请输入密码"
              clearable
              @keyup.enter="onLogin"
          ></el-input>
        </el-form-item>

        <!-- 操作按钮：改为 flex 布局，避免堆叠 -->
        <el-form-item>
          <div class="btn-group">
            <el-button
                type="primary"
                @click="onLogin"
                class="btn-login"
                :loading="loading"
            >
              {{ loading ? '登录中...' : '登 录' }}
            </el-button>
            <el-button @click="resetForm" class="btn-cancel">
              取 消
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {login} from'@/api/user'
export default {
  name: "LoginView",
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      loading: false // 登录加载状态
    }
  },
  methods: {
    onLogin() {
      console.log('发送登陆前信息', this.form)
      this.loading = true
      login(this.form).then(res => {
        console.log('登录返回信息', res)
        if (res.code === 200) {
          // 登录成功
          this.$message.success('登录成功')
          this.$router.push('/admin')
        } else {
          // 登录失败
          this.$message.error('用户名或密码错误')
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(135deg, #6aab73, #2aacb8); /* 鲜艳橙红色渐变 */
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
}

.login-box {
  width: 420px;
  padding: 50px 40px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.login-box:hover {
  transform: translateY(-2px);
  box-shadow: 0 18px 45px rgba(0, 0, 0, 0.25);
}

.login-title {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 36px;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.login-form :deep(.el-form-item__label) {
  font-weight: 500;
  color: #2c3e50;
  font-size: 14px;
}

.login-form :deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
}

.login-form :deep(.el-input__wrapper):hover {
  box-shadow: 0 2px 8px rgba(255, 107, 107, 0.2);
}

.login-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 2px 10px rgba(255, 107, 107, 0.3);
}

/* 按钮组：使用 flex 布局，让两个按钮并排 */
.btn-group {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 20px;
}

/* 登录按钮 */
.btn-login {
  flex: 1;
  height: 42px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  background: linear-gradient(to right, #6aab73, #2aacb8);
  border: none;
  border-radius: 8px;
  transition: all 0.3s;
  color: white;
}

.btn-login:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(255, 107, 107, 0.3);
}

/* 取消按钮 */
.btn-cancel {
  flex: 1;
  height: 42px;
  color: #777;
  border-color: #ddd;
  font-size: 15px;
  border-radius: 8px;
  transition: all 0.2s;
}

.btn-cancel:hover {
  color: #444;
  border-color: #bbb;
  background: #f9f9f9;
}
</style>